//将用户在周末预定的任意时间端转化为标准时间段来收费，输出该时间段用户的费用
function calculationWeekendTime(firstTime, lastTime, weekend) {
    if (firstTime >= 12) {
        if (firstTime >= 18) {
            return (lastTime - firstTime) * weekend["22"];
        } else {
            if (lastTime > 18) {
                return (18 - firstTime) * weekend["18"] + (lastTime - 18) * weekend["22"];
            } else {
                return (lastTime - firstTime) * weekend["18"];
            }
        }
    } else {
        if (lastTime > 12) {
            if (lastTime > 18) {
                return (12 - firstTime) * weekend["12"] + (18 - 12) * weekend["18"] + (lastTime - 18) * weekend["22"];
            } else {
                return (12 - firstTime) * weekend["12"] + (lastTime - 12) * weekend["18"];
            }
        } else {
            return (lastTime - firstTime) * weekend["12"];
        }
    }
}

//将用户在工作日预定的任意时间端转化为标准时间段来收费，输出该时间段用户的费用
function calculationWorkdayTime(firstTime, lastTime, workday) {
    if (firstTime >= 12) {
        if (firstTime >= 18) {
            if (firstTime >= 20) {
                return (lastTime - firstTime) * workday["22"];
            } else {
                if (lastTime > 20) {
                    return (20 - firstTime) * workday["20"] + (lastTime - 20) * workday["22"];
                } else {
                    return (lastTime - firstTime) * workday["20"];
                }
            }
        } else {
            if (lastTime > 18) {
                if (lastTime > 20) {
                    return (18 - firstTime) * workday["18"] + (20 - 18) * workday["20"] + (lastTime - 20) * workday["22"];
                } else {
                    return (18 - firstTime) * workday["18"] + (lastTime - 18) * workday["20"];
                }
            } else {
                return (lastTime - firstTime) * workday["18"];
            }
        }
    } else {
        if (lastTime > 12) {
            if (lastTime > 18) {
                if (lastTime > 20) {
                    return (12 - firstTime) * workday["12"] + (18 - 12) * workday["18"] + (20 - 18) * workday["20"] + (lastTime - 18) * workday["22"];
                } else {
                    return (12 - firstTime) * workday["12"] + (18 - 12) * workday["18"] + (lastTime - 18) * workday["20"];
                }
            } else {
                return (12 - firstTime) * workday["12"] + (lastTime - 12) * workday["18"];
            }
        } else {
            return (lastTime - firstTime) * workday["12"];
        }
    }
}

//分割用户的时间并计算价格的函数
function splitTime(customerTimeList) {
    let Todayincome = {
        "cancelled": [],
        "booking": []
    };
    if (customerTimeList.today === "weekend") {
        let weekend = {
            "cancel": 0.25,
            "12": 40,
            "18": 50,
            "22": 60
        };
        if (customerTimeList.hasOwnProperty("cancelled") && customerTimeList.cancelled.length > 0) {
            for (let i = 0; i < customerTimeList.cancelled.length; i++) {
                let firstTime = parseInt(customerTimeList.cancelled[i].split("~")[0]);
                let lastTime = parseInt(customerTimeList.cancelled[i].split("~")[1]);
                let cost = calculationWeekendTime(firstTime, lastTime, weekend) * weekend.cancel;
                Todayincome.cancelled.push([customerTimeList.cancelled[i], cost]);
            }
        }
        if (customerTimeList.hasOwnProperty("usedTime") && customerTimeList.usedTime.length > 0) {
            for (let i = 0; i < customerTimeList.usedTime.length; i++) {
                let firstTime = parseInt(customerTimeList.usedTime[i].split("~")[0]);
                let lastTime = parseInt(customerTimeList.usedTime[i].split("~")[1]);
                let cost = calculationWeekendTime(firstTime, lastTime, weekend);
                Todayincome.booking.push([customerTimeList.usedTime[i], cost]);
            }
        }
        return Todayincome;
    } else if (customerTimeList.today === "workday") {
        let workday = {
            "cancel": 0.5,
            "12": 30,
            "18": 50,
            "20": 80,
            "22": 60
        };
        if (customerTimeList.hasOwnProperty("cancelled") && customerTimeList.cancelled.length > 0) {
            for (let i = 0; i < customerTimeList.cancelled.length; i++) {
                let firstTime = parseInt(customerTimeList.cancelled[i].split("~")[0]);
                let lastTime = parseInt(customerTimeList.cancelled[i].split("~")[1]);
                let cost = calculationWorkdayTime(firstTime, lastTime, workday) * workday.cancel;
                Todayincome.cancelled.push([customerTimeList.cancelled[i], cost]);
            }
        }
        if (customerTimeList.hasOwnProperty("usedTime") && customerTimeList.usedTime.length > 0) {
            for (let i = 0; i < customerTimeList.usedTime.length; i++) {
                let firstTime = parseInt(customerTimeList.usedTime[i].split("~")[0]);
                let lastTime = parseInt(customerTimeList.usedTime[i].split("~")[1]);
                let cost = calculationWorkdayTime(firstTime, lastTime, workday);
                Todayincome.booking.push([customerTimeList.usedTime[i], cost]);
            }
        }
        return Todayincome;
    }
}

//给splitTime()提供数据的函数
function splitCustomerList(customerList) {
    let obj = {};
    for (let space in customerList) {
        for (let date in customerList[space]) {
            obj[space] = {
                [date]: splitTime(customerList[space][date])
            };
        }
    }
    return obj;
}

//打印用户的收入汇总的主函数
function printIncome(customerList) {
    let outPutString = '收入汇总\n---';
    let income = splitCustomerList(customerList);
    let sum = 0;
    for (let space in income) {
        outPutString += "\n场地：" + space + "\n";
        let spaceSum = 0;
        for (let date in income[space]) {
            let cancel = income[space][date].cancelled;
            let booking = income[space][date].booking;
            for (let i = 0; i < cancel.length; i++) {
                sum += cancel[i][1];
                spaceSum += cancel[i][1];
                outPutString += date + " " + cancel[i][0] + " " + "违约金" + " " + cancel[i][1] + '元' + '\n';
            }
            for (let i = 0; i < booking.length; i++) {
                spaceSum += booking[i][1];
                sum += booking[i][1];
                outPutString += date + " " + booking[i][0] + " " + booking[i][1] + '元' + '\n';
            }
        }
        outPutString += '小计：' + spaceSum + '元\n';
    }
    outPutString += '---\n总计 ' + sum + '元';
    return outPutString;
}

module.exports.printIncome = printIncome;